﻿namespace MessengerSample
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;

    using Microsoft.Live.Messenger;

    public class ConversationToConversationNameConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            IMAddressCollection roster = (IMAddressCollection)value;

            if (roster.Count == 2)
            {
                foreach (IMAddress address in roster)
                {
                    if (!address.IsUser)
                    {
                        string displayName = address.Presence.DisplayName;

                        if (!String.IsNullOrEmpty(displayName))
                        {
                            return displayName;
                        }

                        return address.Address;
                    }
                }
            }

            return "Multiparty";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}